package com.study.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.Service;
import com.study.model.pojo.User;
import com.study.model.pojo.WeixiuCart;
import com.study.services.CustomerService;
import com.study.services.DeptService;
import com.study.services.DictionaryService;
import com.study.services.LinkmanService;
import com.study.services.ServiceSerivce;
import com.study.services.UserService;
import com.study.services.WeixiuService;

@Controller
public class ServiceController {
	@Autowired
	ServiceSerivce sservice;
	@Autowired
	CustomerService custService;
	@Autowired
	DictionaryService dservice;
	@Autowired
	LinkmanService lservice;
	@Autowired
	UserService uservice;
	@Autowired
	LinkmanService linkmanService;
	@Autowired
	DeptService deptService;

	@GetMapping("/servicetys")
	public ModelAndView getForm5(HttpSession session) {
		ModelAndView mv = new ModelAndView("afterSale/kehufuwu");
		List<JSONObject> list = sservice.selectBySel(1, 6, "", "","");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		mv.addObject("servicetys", json);
		return mv;
	}

	/**
	 * 查看服务（第一次进入）
	 */
	@RequestMapping("servicety")
	@ResponseBody
	public String service1() {
		List<JSONObject> list = sservice.selectBySel(1, 6, "", "","");
		// pagerhelper自定的PageInfo类.会把ArrayList的信息，整合到PageInfo对象中的list属性中。JSON亦然
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		// System.out.println(json);
		return json;
	}

	@GetMapping("servicety2")
	public ModelAndView getForm2() {
		ModelAndView mv = new ModelAndView("/afterSale/addservice");
		mv.addObject("service", new JSONObject());
		return mv;
	}

	/**
	 * 获客采集分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("afterSale/kehufuwu")
	@ResponseBody
	public String getForm3(String input, String selects, int currentPage, int pagesize) {
		// System.out.println(currentPage + "currentPage");
		// System.out.println(pagesize + "pagesize");
		// System.out.println(input);
		// System.out.println(selects);

		List<JSONObject> list = sservice.selectBySel(currentPage, pagesize, input.trim(), selects.trim(),"");

		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		String json = JSONObject.toJSONString(pager);

		return json;
	}

	/**
	 * 添加
	 */
	@PostMapping("serviceform")
	@ResponseBody
	public boolean handleForm(String name, String custid, String type, String mode, Date date1, String spendtime,
			String linId, String state, String content, String back, String more, HttpSession session) {

		Integer useid = (Integer) session.getAttribute("useid");

		boolean result = sservice.addService(name, custid, type, mode, date1, spendtime, linId, state, useid+"", content,
				back, more);

		System.out.println(result + "我是售后服务的结果");
		return result;
		
	}

	/**
	 * 编辑
	 * 
	 * @param name
	 * @param custname
	 * @param type
	 * @param mode
	 * @param date1
	 * @param date2
	 * @param spendtime
	 * @param linkman
	 * @param state
	 * @param user
	 * @param content
	 * @param back
	 * @param more
	 * @return
	 */
	@PostMapping("editerserviceform")
	@ResponseBody
	public boolean editorForm(String name, String custid, String type, String mode, Date date1, 
			String spendtime, String linId, String state, String user, String content, String back, String more,
			String id, HttpSession session) {
		Integer useid = (Integer) session.getAttribute("useid");
		boolean result = sservice.editorService(name, custid, type, mode, date1, spendtime, linId, state, useid, content,
				back, more, id);
		System.out.println(result + "我是售后服务的结果s");
		return result;
	}

	/**
	 * 编辑
	 */
	@GetMapping("servicEditor")
	public ModelAndView addCustomerByColle(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		JSONObject service = sservice.selectBycid(parameter);
		ModelAndView mv = new ModelAndView("/afterSale/addservice");
		mv.addObject("service", service);
		System.out.println(service + "我是一条服务");
		return mv;
	}

	/**
	 * 删除服务
	 * 
	 * @param goo_id
	 * @return
	 */
	@PostMapping("/delService")
	@ResponseBody
	public String delete(Integer id) {
		System.out.println(id + "删除服务的id");
		sservice.delete(id);
		return "ok";
	}

	/**
	 * 售后服务
	 */
	@GetMapping("getService")
	@ResponseBody
	public ModelAndView getForm21(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("/afterSale/kehufuwu");
		String type = request.getParameter("type");
		if (type != null) {
			session.setAttribute("type", type);
		}
		mv.addObject("servicemode", sservice.selectByMode());
		mv.addObject("servicecount", sservice.selectByCount());
		mv.addObject("type", "type");
		return mv;
	}

	/**
	 * 客服总控台 根据信息模糊查询数据 queryMesssage
	 */
	@PostMapping("/queryMesssage")
	@ResponseBody
	public JSONObject queryByLike(String msg) {
		System.out.println("我是客服总控台的查询" + msg);
		JSONObject object = new JSONObject();
		// 根据姓名模糊查询客户
		List<JSONObject> cust1 = custService.findByLike(msg);
		object.put("likeCust", cust1);
		System.out.println("根据姓名模糊查询客户");
		System.out.println(cust1);

		// 根据电话号码查询客户
		List<JSONObject> cust2 = custService.findByPhone(msg);
		System.out.println("根据电话号码查询客户");
		object.put("likePhone", cust2);

		//根据订单号查询可添加维修订单的
		List<WeixiuCart> orders = sservice.queryByLike(msg);
		
		System.out.println("根据订单号查询可添加维修订单的");
		System.out.println(orders);
		object.put("orders", orders);

		return object;
	}

	@GetMapping("/queryCustId")
	public ModelAndView queryCustId(HttpServletRequest request) {
		String parameter = request.getParameter("custId");
		Integer i = Integer.parseInt(request.getParameter("i"));
		System.out.println(parameter+"----客服"+i);
		ModelAndView mv=null;
		if (i == 3) {
			System.out.println("我是维修");
			mv = new ModelAndView("/afterSale/serviceTableToWeixiu");
			JSONObject good = new JSONObject();
			good.put("goo_id", request.getParameter("goodId"));
			good.put("goo_name", request.getParameter("goodName"));
			
			JSONObject order = new JSONObject();
			order.put("o_id", request.getParameter("orderId"));
			order.put("otitle", request.getParameter("orderName"));
			
			mv.addObject("dept", deptService.findDeptByPar(3));
			mv.addObject("good", good);
			mv.addObject("order", order);
			mv.addObject("weixiuLess", new JSONObject());
		}else if(i == 1) {
			System.out.println("我是服务");
			mv = new ModelAndView("/afterSale/serviceTableToService");
		}else if(i == 2) {
			System.out.println("我是投诉");
			mv = new ModelAndView("/afterSale/serviceTableToTousu");
		}else if(i == 5) {
			System.out.println("我是客户信息");
			mv = new ModelAndView("/timeline_v2");
			mv.addObject("time", custService.selectBytime(parameter,null));
			// 时间记录
			mv.addObject("timeCount", custService.findTimeByCount(parameter));
		}else {
			System.out.println("我是其他");
			mv = new ModelAndView("/afterSale/serviceTableToService");
		}
		mv.addObject("service", custService.queryCus(parameter));
		return mv;
	}
}
